package study.domain.user;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Data;

import org.springframework.security.core.GrantedAuthority;

import com.barney.support.domain.HasCreatedDate;
import com.barney.support.jpa.CreatedDateListener;

/**
 * 권한
 *
 * @author Barney Kim
 */
@Entity(name = "authorities")
@EntityListeners(CreatedDateListener.class)
@Data
public class Authority implements Serializable, GrantedAuthority, HasCreatedDate {

	private static final long serialVersionUID = 1087013002255068256L;

	@EmbeddedId
	private AuthorityId id;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false)
	private Date createdDate;

	@Override
	public String getAuthority() {
		return id.getRole().getRole();
	}

	public Authority() {
		//
	}

	public Authority(User user, Role role) {
		this(new AuthorityId(user, role));
	}

	public Authority(AuthorityId id) {
		this.id = id;
	}

}
